package com.xq.springboot.filter;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/19 14:45
 * @description shiro的登录过滤器类
 */
public class ShiroLoginFilter extends FormAuthenticationFilter {
    /**
     * 登录未认证的方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        Map<String, Object> resultDate = new HashMap<>(16);
        resultDate.put("code", -1);
        resultDate.put("msg","未登录");
        httpServletResponse.getWriter().write(JSONObject.toJSONString(resultDate).toString());
        return false;
    }

}
